package random.fox.p2p.interactors;

import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.json.JSONObject;

import javax.inject.Inject;

import random.fox.p2p.App;
import random.fox.p2p.api.ApiClient;
import random.fox.p2p.bean.BeanModule;
import random.fox.p2p.bean.UserInfo;
import random.fox.p2p.bean.greendao.User;

/**
 * Created by acer on 2015/3/22.
 */
public class UserInfoInteractorImpl implements UserInfoInteractor {

    private App app;
    @Inject
    User user;
    private UserInfo userInfo;

    public UserInfoInteractorImpl(App app){
        this.app = app;
        app.createScopedGraph(new BeanModule()).inject(this);
    }

    @Override
    public void getData(final OnLoadNetDataListener listener) {
        ApiClient apiClient = app.getApiClient();
        apiClient.getUserInfo(new ApiClient.CallBack<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                String back = o.toString();
                userInfo = new Gson().fromJson(back, UserInfo.class);
                listener.onSuccess(userInfo);
                System.out.println("TT onResponse:"+System.currentTimeMillis());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                listener.onFailed("OnLoadNetDataListener:加载失败");
            }
        });
    }

    @Override
    public User getUser() {
        return user;
    }


    @Override
    public UserInfo getCacheUserInfo() {
        return userInfo;
    }

    @Override
    public void releaseData() {
        userInfo = null;
    }

//    private UserInfo initUserInfo() {
//        String arrs[] = {"面基达人", "网坛美少女", "瑜伽圣斗士", "学霸", "体贴的萌妹子", "山本五十二", "5年中考", //7
//                "古有照妖镜", "今有卸妆油", "深巷已无故人", "事过境迁", "物是人非", "别说后来", "你又不在", "人丑多读书", //8
//                "冬天里的小欧巴", "无坚不摧的背后", "是不堪一击", "不要吻我", "滚开我有神经病", "没事宝贝我有药", //6
//                "亡与栀枯", "潜力股"};
//
//        List<PersonalityLabel> personalityLabels = new ArrayList<>();
//        for (int i = 0; i < 12; i++) {
//            PersonalityLabel personalityLabel = new PersonalityLabel();
//            personalityLabel.setLable(arrs[i]);
//            personalityLabel.setCount((i + 2) * 4);
//            personalityLabels.add(personalityLabel);
//        }
//
//        List<Dynamic> dynamics = new ArrayList<Dynamic>();
//        for (int i = 0; i < 18; i++) {
//            Dynamic dynamic = new Dynamic();
//            long i1= i * 1000 * 3600 * 20;
//            long milliseconds = System.currentTimeMillis() - i1;
//            dynamic.setDate(new Date(milliseconds));
//            ArrayList<String> urls = new ArrayList<>();
//            for (int x = 0; x < 3; x++)
//                urls.add(Constants.IMAGES[i]);
//            dynamic.setUrls(urls);
//
//            dynamic.setTitle("这是标题" + i);
//            dynamic.setContent("这是内容这是内容这是内容这是内容:" + i);
//            dynamics.add(dynamic);
//        }
//
//        List<Fans> fansList = new ArrayList<>();
//        for (int i = 0; i < 13; i++) {
//            Fans fans = new Fans(i, "我是" + i, Constants.IMAGES[i+14]);
//            fansList.add(fans);
//        }
//        UserInfo userInfo = new UserInfo("大豌豆", null, personalityLabels, dynamics, fansList);
//
//        Gson gson = new Gson();
//        String s = gson.toJson(userInfo);
//
//        FileOutputStream out = null;
//        try {
//            out= new FileOutputStream(new File("/sdcard/a.txt"));
//
//            out.write(s.getBytes());
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                out.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        Log.d("FFF", s);
//
//        return userInfo;
//    }
}
